<template>
  <div class="login">
    <!-- <div class="login-title">
      
      <img src="https://web.nbyongxiang.cn/minioczsh/2025/3/1743056463779商会logo2.png">
      中财墙板AI系统后台
    </div> -->
    <!-- <div class="login-left"></div> -->
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <!-- <h3 class="title">中财墙板AI系统后台</h3> -->
      <div class="top-view-box">
        <div class="logo-container">
          <img src="@/assets/logo/logo.png" class="logo-img" />
          <div class="system-title">（六安）中财墙纸AI后台管理</div>
          <div class="welcome-text">欢迎登录</div>
        </div>
      </div>
      <div class="form-container">
          <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
            @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;font-size: 14px;color: #374151;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="large" type="primary" style="width:100%;background: #025946;height: 48px;"
            @click.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </div>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>© 2025 六安中财管道科技有限公司</span>
    </div>
    <div class="footer-beian">
      <!-- <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">浙ICP备2025150483号-2</a> -->
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'
import CryptoJS from 'crypto-js';
const userStore = useUserStore()
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const publicKey = import.meta.env.VITE_APP_PUBLICKEY; //引入公钥
const loginForm = ref({
  username: "yxadmin",
  // password: "admin123",
  password: "yx@123456",
  rememberMe: false,
  code: "",
  uuid: ""
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", CryptoJS.AES.decrypt(loginForm.value.username, publicKey).toString(CryptoJS.enc.Utf8), { expires: 30 });
        Cookies.set("password", CryptoJS.AES.decrypt(loginForm.value.password, publicKey).toString(CryptoJS.enc.Utf8), { expires: 30 });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法 
      userStore.login(loginForm.value).then(() => {
        const query = route.query;
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur];
          }
          return acc;
        }, {});
        router.push({ path: redirect.value || "/", query: otherQueryParams });
      }).catch(() => {
        loading.value = false;
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCode();
getCookie();
</script>


<style lang='scss' scoped>
.top-view-box {
  width: 100%;
  height: 176px;
  background: linear-gradient(140deg, #03755C 0%, #025946 100%);
  border-radius: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
  
  .logo-container {
    text-align: center;
    
    .logo-img {
      width: 64px;
      height: 64px;
      margin-bottom: 12px;
    }
    
    .system-title {
      font-weight: normal;
      font-size: 20px;
      color: #FFFFFF;
      line-height: 28px;
      margin-bottom: 4px;
    }
    
    .welcome-text {
      font-size: 14px;
      color: rgba(255,255,255,0.8);
      line-height: 20px;
    }
  }
}

.footer-beian {
  position: fixed;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 14px;
  letter-spacing: 2px;
  z-index: 10;
  font-family: "Microsoft YaHei", sans-serif;

  a {
    display: inline-block;
    padding: 8px 16px;
    color: #e8f4f4; // 浅青白色
    text-decoration: none;
    // background-color: rgba(37, 106, 106, 0.7);  // 使用主题色半透明
    // border-radius: 12px;  // 更圆润的边框
    // transition: all 0.3s ease;
    // border: 1px solid rgba(200, 230, 230, 0.3);  // 添加浅色边框
    box-shadow: 0 2px 8px rgba(37, 106, 106, 0.3); // 青色阴影

    &:hover {
      color: #fff;
      background-color: rgba(37, 106, 106, 0.9); // 加深主题色
      transform: translateY(-3px);
      box-shadow: 0 4px 12px rgba(37, 106, 106, 0.4); // 悬浮时阴影增强
      border-color: rgba(255, 255, 255, 0.5); // 悬浮时边框变亮
    }
  }

  &::before {
    content: "";
    display: block;
    width: 60%;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(37, 106, 106, 0.5), transparent);
    margin: 0 auto 10px;
  }
}

.login-title {
  position: fixed;
  top: 40px;
  left: 22%;
  transform: translateX(-50%);
  font-size: 38px;
  /* 增大字体大小 */
  letter-spacing: 5px;
  /* 增加字间距 */
  color: #ffff;
  /* 更柔和的文字颜色 */
  font-weight: bold;
  text-transform: uppercase;
  /* 文本转换为大写 */
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
  /* 添加文字阴影 */
  font-family: "Roboto", sans-serif;
  display: flex;
  align-items: center;

  /* 使用适合后台管理系统的大标题字体 */
  img {
    height: 50px;
    width: 50px;
  }
}


.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  // background-image: url("../assets/images/login-background.png");
  // background-image: url("../assets/images/login-background2.jpg");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background: linear-gradient(119deg, #03755C 0%, #025946 50%, #017A6B 100%), #FFFFFF;
}

// .login{
//     display: flex;
//   justify-content: center;
//   align-items: center;
//   min-height: 100%;  // 改为min-height确保内容不足时也能撑开
//   background-image: url("../assets/images/login-background2.jpg");
//   background-size: cover;
//   background-position: center;  // 添加居中定位
//   background-attachment: fixed; // 固定背景图
//   background-repeat: no-repeat;
//   // padding: 20px; // 添加内边距防止内容贴边
// }
.login-left {
  position: fixed;
  top: 10vh;
  left: 10%;
  height: 680px;
  width: 680px;
  background-image: url("../assets/images/bg-left.png");
  background-size: cover;
  animation: floatAnimation 4s ease-in-out infinite alternate;
  /* 添加浮动动画效果 */
}

@keyframes floatAnimation {
  0% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
    /* 上浮10像素 */
  }

  100% {
    transform: translateY(0);
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #333333;
  font-weight: bold;
  font-size: 26px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 425px;
  // padding: 35px 25px 25px 25px;
  position: fixed;
  // right: 180px;
  .form-container{
    padding: 35px 25px 25px 25px;
  }
  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 20px;
  width: 100%;
  text-align: center;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 0.6);
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>